package org.wishwork.api.stock.in.upload;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import org.wishwork.api.Api;
import org.wishwork.api.stock.in.upload.request.Item;
import org.wishwork.api.stock.in.upload.request.StockInUploadRequest;
import org.wishwork.context.Context;
import org.wishwork.helper.HttpHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 新建采购入库单（新）
 * @author: WangYX
 * @create: 2022-04-11 19:34
 * @Version: 1.0.0
 **/

public class StockInUploadApi extends Api<StockInUploadResponse, StockInUploadRequest> {

    private Context context;

    public StockInUploadApi(Context context) {
        this.context = context;
    }

    @Override
    protected String query(StockInUploadRequest stockInUploadRequest) throws Exception {
        String url = "/open/jushuitan/purchasein/upload";
        Map<String, Object> params = new HashMap<>();
        if (ObjectUtil.isNotEmpty(stockInUploadRequest.getIsConfirm())) {
            params.put("is_confirm", stockInUploadRequest.getIsConfirm());
        }
        if (stockInUploadRequest.getWmsCoId() != null) {
            params.put("wms_co_id", stockInUploadRequest.getWmsCoId());
        }
        params.put("supplier_id", stockInUploadRequest.getSupplierId());
        if (stockInUploadRequest.getPoId() != null) {
            params.put("po_id", stockInUploadRequest.getPoId());
        }
        params.put("external_id", stockInUploadRequest.getExternalId());
        if (StrUtil.isNotEmpty(stockInUploadRequest.getRemark())) {
            params.put("remark", stockInUploadRequest.getRemark());
        }
        if (ObjectUtil.isNotEmpty(stockInUploadRequest.getTaxRate())) {
            params.put("tax_rate", stockInUploadRequest.getTaxRate());
        }
        if (stockInUploadRequest.getCoId() != null) {
            params.put("co_id", stockInUploadRequest.getCoId());
        }
        List<Map<String,Object>> items = new ArrayList<>();
        if (stockInUploadRequest.getItems()!=null&&stockInUploadRequest.getItems().size()>0) {
            for (Item item : stockInUploadRequest.getItems()) {
                Map<String,Object> map = new HashMap<>();
                map.put("sku_id",item.getSkuId());
                map.put("qty",item.getQty());
                if (item.getSkuSn()!=null&&item.getSkuSn().size()>0) {
                    map.put("sku_sn",item.getSkuSn());
                }
                if(ObjectUtil.isNotEmpty(item.getPrice())){
                    map.put("price",item.getPrice());
                }
                if (StrUtil.isNotEmpty(item.getBatchId())) {
                    map.put("batch_id",item.getBatchId());
                }
                if (StrUtil.isNotEmpty(item.getProducedDate())) {
                    map.put("produced_date",item.getProducedDate());
                }
                if (StrUtil.isNotEmpty(item.getExpirationDate())) {
                    map.put("expiration_date",item.getExpirationDate());
                }
                if (StrUtil.isNotEmpty(item.getRemark())) {
                    map.put("remark",item.getRemark());
                }
                items.add(map);
            }
            params.put("items",items);
        }
        return HttpHelper.sendRequest(params, url);
    }

    @Override
    protected StockInUploadResponse process(JSONObject data, StockInUploadRequest stockInUploadRequest) throws Exception {
        return data.toJavaObject(StockInUploadResponse.class);
    }
}
